﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UboxUI;

namespace UboxRobot
{
    public partial class FrmCompareData : FrmFather
    {
        public FrmCompareData()
        {
            InitializeComponent();
        }

        List<ProductID> PhoneID;
        
        Random r = new Random();
        private async void button1_Click(object sender, EventArgs e)
        {
            UboxContext db = new UboxContext();

            this.Cursor = Cursors.WaitCursor;
            PhoneID = (db.production_product.Where(p => p.category_id == 1).Select(p => new ProductID { ID = p.product_id })).ToList();
            int last = PhoneID.Count - 1;
            int ID = PhoneID[last].ID;

            deleteData(ID);

            this.progressBar.Value = 20;
            #region 2
            // 2
            for (int i = 0; i < PhoneID.Count; i++)
            {
                int p1 = PhoneID[i].ID;
                int p2 = ID;
                if (p2 > p1)
                {
                    int countR = Convert.ToInt32(r.NextDouble() * 100);
                    var d = new compare_2_products
                    {
                        product_1 = p1,
                        product_2 = p2,
                        count = countR
                    };
                    db.compare_2_products.Add(d);
                }
            }

            this.progressBar.Value = 40;
            #endregion
            #region 3
            // 3
            for (int i = 0; i < PhoneID.Count; i++)
            {
                for (int j = i + 1; j < PhoneID.Count; j++)
                {
                    int p1 = PhoneID[i].ID;
                    int p2 = PhoneID[j].ID;
                    int p3 = ID;
                    if (p3 > p2 && p2 > p1)
                    {
                        int countR = Convert.ToInt32(r.NextDouble() * 100);
                        var d = new compare_3_products
                        {
                            product_1 = p1,
                            product_2 = p2,
                            product_3 = p3,
                            count = countR
                        };
                        db.compare_3_products.Add(d);
                    }
                }
            }

            this.progressBar.Value = 60;
            #endregion
            #region 4
            // 4
            for (int i = 0; i < PhoneID.Count; i++)
            {
                for (int j = i + 1; j < PhoneID.Count; j++)
                {
                    for (int k = j + 1; k < PhoneID.Count; k++)
                    {
                        int p1 = PhoneID[i].ID;
                        int p2 = PhoneID[j].ID;
                        int p3 = PhoneID[k].ID;
                        int p4 = ID;
                        if (p4 > p3 && p3 > p2 && p2 > p1)
                        {
                            int countR = Convert.ToInt32(r.NextDouble() * 100);
                            var d = new compare_4_products
                            {
                                product_1 = p1,
                                product_2 = p2,
                                product_3 = p3,
                                product_4 = p4,
                                count = countR
                            };
                            db.compare_4_products.Add(d);
                        }
                    }
                }
            }

            this.progressBar.Value = 80;
            #endregion
            #region 5
            // 5
            for (int i = 0; i < PhoneID.Count; i++)
            {
                for (int j = i + 1; j < PhoneID.Count; j++)
                {
                    for (int k = j + 1; k < PhoneID.Count; k++)
                    {
                        for (int m = k + 1; m < PhoneID.Count; m++)
                        {
                            int p1 = PhoneID[i].ID;
                            int p2 = PhoneID[j].ID;
                            int p3 = PhoneID[k].ID;
                            int p4 = PhoneID[m].ID;
                            int p5 = ID;
                            if (p5 > p4 && p4 > p3 && p3 > p2 && p2 > p1)
                            {
                                int countR = Convert.ToInt32(r.NextDouble() * 100);
                                var d = new compare_5_products
                                {
                                    product_1 = p1,
                                    product_2 = p2,
                                    product_3 = p3,
                                    product_4 = p4,
                                    product_5 = p5,
                                    count = countR
                                };
                                db.compare_5_products.Add(d);
                            }
                        }
                    }
                }
            }
            #endregion
            await db.SaveChangesAsync();
            this.progressBar.Value = 100;

            this.Cursor = Cursors.Default;
            MessageBox.Show("新增商品比拚資料成功！");
        }

        private void deleteData(int ID)
        {
            string strConn = ClassLibrary.login.strconn;
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                string strCmd = @"delete from compare_2_products where product_2 = @id
                                                delete from compare_3_products where product_3 = @id
                                                delete from compare_4_products where product_4 = @id
                                                delete from compare_5_products where product_5 = @id";
                using (SqlCommand cmd = new SqlCommand(strCmd, conn))
                {
                    conn.Open();
                    cmd.Parameters.AddWithValue("@id", ID);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
        }
    }
}
